function createList(pageNo = 1, pageSize = 10) {
    let ownerUid = $('#ownerUid').val() || null;
    let unionID = $('#unionID').val() || null;
    let matchData = {
        'isDel': {
            $ne: true
        }
    };
    if (!!ownerUid) matchData.ownerUid = ownerUid;
    if (!!unionID) matchData.unionID = unionID;

    var start = (pageNo - 1) * pageSize;
    apiAdminGetUnion("unionModel", start, pageSize, matchData, function (data) {
        if (data.code === 0) {
            if (data.code === 0) {
                setTimeout(() => {
                    $('#dataList').bootstrapTable('load', data.msg);
                }, 200)
            }
        }
    });

}

function deleteUnion(unionID) {

    let permission = localStorage.getItem('permission', '');
    if (permission != -1) {
        $.modal({
            body: '权限不足'
        });
        return;
    }

    Swal.fire({
        title: '确定要删除联盟？',
        text: "联盟删除后无法恢复，请谨慎操作",
        icon: 'question',
        showCancelButton: true,
        confirmButtonColor: '#198754',
        cancelButtonColor: '#d93444',
        confirmButtonText: '确定',
        cancelButtonText: '取消'
    }).then((result) => {
        if (result.isConfirmed) {
            apiDeleteUnion(unionID, function () {
                createList();
                $.modal({
                    body: '操作成功'
                });
            });
        }
    });

}

function unionSetMain(unionID, isMain) {
    // let message = '是否确定推荐该联盟';
    // if (isMain == 0) {
    //     message = '是否取消推荐该联盟';
    // }
    // if (!confirm(message)) return;
    apiUnionSetMain(unionID, isMain, function () {
        $.modal({
            body: '操作成功'
        });
        createList(1);
    });
}

$(document).ready(function () {
    // 初始化数据列名
    //数据表格初始化
    $('#dataList').bootstrapTable({
        showRefresh: true,
        striped: true, // 是否显示行间隔色
        pageNumber: 1, // 初始化加载第一页
        pagination: true, // 是否分页
        sidePagination: 'server', // server:服务器端分页|client：前端分页
        pageSize: 10, // 单页记录数
        pageList: [10, 20, 30],
        theadClasses: "thead-light", //这里设置表头样式
        totalField: "totalCount",
        smartDisplay: false,
        buttonsClass: 'light',
        buttonsPrefix: 'btn',
        dataField: "recordArr",
        maintainSelected: true,
        showRefresh: false, // 刷新按钮
        onPageChange: function (number, size) {
            createList(number, size);
        },
        onPostBody: function () {
            //重新启用弹出层,否则formatter格式化函数返回的html字符串上的tooltip提示不生效
            $('[data-bs-toggle="tooltip"]').each(function (i, el) {
                new bootstrap.Tooltip(el)
            })
        },
        onLoadSuccess: function () {
            //重新启用弹出层,否则formatter格式化函数返回的html字符串上的tooltip提示不生效
            $('[data-bs-toggle="tooltip"]').each(function (i, el) {
                new bootstrap.Tooltip(el)
            })
        },
        queryParams: function (params) { // 上传服务器的参数
            var temp = { //如果是在服务器端实现分页，limit、offset这两个参数是必须的
                limit: params.limit, // 每页显示数量
                offset: params.offset, // SQL语句起始索引
            };
            return temp;
        },
        columns: [
            //     {
            //     checkbox: true,
            //     align: "center",
            //     valign: "middle",
            // }, 
            {
                title: 'id',
                field: '_id',
                visible: false
            }, {
                title: '联盟名称',
                width: 200,
                align: "center",
                valign: "middle",
                field: 'unionName',
            },
            // {
            //     title: '联盟类型',
            //     width: 200,
            //     align: "center",
            //     valign: "middle",
            //     field: 'unionType',
            //     formatter: function (value) {
            //         if (value == 1) {
            //             return "普通场"
            //         } else {
            //             return "比赛场"
            //         }
            //     }
            // },
            {
                title: '邀请码',
                width: 150,
                align: "center",
                valign: "middle",
                field: 'unionInviteID',
            },
            {
                title: '联盟ID',
                width: 150,
                align: "center",
                valign: "middle",
                field: 'unionID',
            }, {
                title: '盟主ID',
                width: 150,
                align: "center",
                valign: "middle",
                field: 'ownerUid',
                order: "asc",

            }, {
                title: '盟主昵称',
                width: 150,
                align: "center",
                valign: "middle",
                field: 'ownerNickname',
            }, {
                title: '玩家数量',
                width: 100,
                align: "center",
                valign: "middle",
                field: 'curMember',
            },
            // {
            //     title: '机器人数量',
            //     width: 100,
            //     align: "center",
            //     valign: "middle",
            //     field: 'robotCount',
            // }, 
            {
                title: '是否营业',
                width: 100,
                align: "center",
                valign: "middle",
                field: 'opening',
                formatter: function (value) {
                    return !!value ? "营业" : "打烊";
                }
            },

            // {
            //     title: '赠送积分',
            //     width: 100,
            //     align: "center",
            //     valign: "middle",
            //     field: 'forbidGive',
            //     formatter: function (value) {
            //         return `<a href="../systemSet/systemSettings.html">系统设置</a>`;
            //     }
            // },
            {
                title: '创建时间',
                width: 200,
                align: "center",
                valign: "middle",
                field: 'createTime',
                formatter: function (value) {
                    return new Date(value).toLocaleString();
                }
            },
            // {
            //     title: '用户管理',
            //     width: 100,
            //     align: "center",
            //     valign: "middle",
            //     field: 'forbidGive',
            //     formatter: goUser,
            // },
            // {
            //     title: '机器人管理',
            //     width: 100,
            //     align: "center",
            //     valign: "middle",
            //     field: 'forbidGive',
            //     formatter: goRobot,
            // },
            {
                title: '钻石消耗',
                width: 100,
                align: "center",
                valign: "middle",
                visible: true,
                field: 'forbidGive',
                formatter: goPay,
            },
            {
                title: '是否推荐',
                width: 50,
                align: "center",
                valign: "middle",
                field: 'isMain',
                visible: false,
                formatter: unionMain
            },
            {
                title: '操作',
                width: 150,
                align: "center",
                valign: "middle",
                formatter: option
            }
        ]
    });
    $('#dataList').bootstrapTable('hideLoading');
    createList(1);
});


function unionMain(value, row, index) {
    var htm = "";
    // if (value) {
    //     htm = `<span class=" badge text-bg-success">已推荐</span>`;
    // } else {
    //     htm = `否`;
    // }
    var htm = "";
    if (value > 0) {
        htm = `<div id="union"  onClick='unionSetMain(${row.unionID},0)' class="simple-switch active"><input type="checkbox" /> <span class="switch-handler"></span> </div>`;
    } else {
        htm = `<div id="union"  onClick='unionSetMain(${row.unionID},1)' class="simple-switch"><input type="checkbox" /> <span class="switch-handler"></span> </div>`;
    }

    return htm;

}


function goUser(value, row, index) {

    //  var htm = `<button class="btn btn-sm btn-outline-primary" onclick="parent.Quicktab.get('.qtab').addTab({title:'联盟用户',url:'html/union/unionUser.html?unionID=${row.unionID}'})">查看用户</button>`
    var htm = `<button type="button" class="btn" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="查看联盟用户" onclick="parent.Quicktab.get('.qtab').addTab({title:'联盟用户',url:'html/union/unionUser.html?unionID=${row.unionID}&unionType=${row.unionType}'})"><i class="bi bi-people-fill fs-3 text-primary"></i></button>`
    return htm;
}

function goRobot(value, row, index) {

    // var htm = `<button class="btn btn-sm btn-outline-primary" onclick="parent.Quicktab.get('.qtab').addTab({title:'联盟机器人',url:'html/union/unionRobot.html?unionID=${row.unionID}'})">查看机器人</button>`

    var htm = `<button type="button" class="btn" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="查看联盟机器人" onclick="parent.Quicktab.get('.qtab').addTab({title:'联盟机器人',url:'html/union/unionRobot.html?unionID=${row.unionID}'})"><i class="bi bi-android2 fs-3 text-success"></i></button>`
    return htm;
}

function goPay(value, row, index) {

    // var htm = `<button class="btn btn-sm btn-outline-primary" onclick="parent.Quicktab.get('.qtab').addTab({title:'联盟机器人',url:'html/union/unionRobot.html?unionID=${row.unionID}'})">查看机器人</button>`

    var htm = `<button type="button" class="btn" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="查看钻石消耗" onclick="parent.Quicktab.get('.qtab').addTab({title:'联盟钻石消耗',url:'html/union/unionPay.html?unionID=${row.unionID}'})"><i class="bi bi-gem fs-4 text-success"></i></button>`
    return htm;
}

// 定义删除、更新按钮
function option(value, row, index) {
    var htm = "";

    // `<button type = "button" class="btn btn-light"  data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="赠送钻石" onClick = 'showAddGold(${row.uid})' ><i class="bi bi-gem fs-5 text-success"></i></button > `;
    // if (parseInt(row.isMain) > 0) {
    //     htm += `<button type="button" class="btn btn-light mx-1" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="取消推荐联盟" onClick='unionSetMain(${row.unionID},0)' ><i class="bi bi-x-square-fill fs-5 text-danger"></i></button>`;

    // } else {
    //     //  htm += `<button type="button" class="btn btn-success btn-sm" onClick="unionSetMain(${row.unionID},1)">推荐联盟</button>`;
    //     htm += `<button type="button" class="btn btn-light mx-1" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="推荐联盟" onClick='unionSetMain(${row.unionID},1)' ><i class="bi bi-box2-heart-fill fs-5 text-success"></i></button>`;
    // }
    //htm += `<button type="button" class="btn" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="查看联盟用户" ><i class="bi bi-people-fill fs-3 text-primary"></i></button>`
    htm += `<button type="button" class="btn btn-light mx-1" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="联盟用户" onclick="parent.Quicktab.get('.qtab').addTab({title:'联盟用户',url:'html/union/unionUser.html?unionID=${row.unionID}&unionType=${row.unionType}'})"><i class="bi bi-people-fill fs-5 text-success"></i></button>`
    //  htm += `  <button type="button" class="btn btn-danger btn-sm" onClick="deleteUnion(${row.unionID},9)">删除</button>`;
    htm += `<button type="button" class="btn btn-light mx-1" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="删除联盟" onClick='deleteUnion(${row.unionID},9)' ><i class="bi bi-trash3-fill fs-5 text-danger"></i></button>`;

    return htm;
}